<template>
	<view class="container">
		<view style="height: 300rpx;"></view>

		<uni-forms ref="form" :rules="rules" :modelValue="loginParams">
			<uni-forms-item class="login-user" name="username">
				<uni-easyinput type="text" v-model="loginParams.username" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item class="login-password" name="password">
				<uni-easyinput type="password" v-model="loginParams.password" placeholder="请输入密码" />
			</uni-forms-item>
			<uni-forms-item class="login-user" name="Offline">

				<radio :checked="isChecked" @tap="()=>isChecked = !isChecked" style="color: #fff;"  active-background-color="#3f6212">离线登录</radio>
			</uni-forms-item>
		</uni-forms>
		<button class="login" @click="submit('form')">登录</button>
		<button class="setup" @click="setupHandle()">设置</button>
	</view>
</template>

<script>
	import {login} from '../../api/login.js'
	import {
		setStorageSync,
		useRouter,
	
	} from '../../utils/utils.js'
	import {
		log
	} from '../../common/util.js'
	export default {
		data(){
			return{
				isChecked:false,
				rules: {
					// 对name字段进行必填验证
					username: {
						rules: [{
								required: true,
								errorMessage: '请输入姓名',
							},
						]
					},
					// 对email字段进行必填验证
					password: {
						rules: [{
								required: true,
								errorMessage: '请输入密码',
							},
				
						]
					}
				},
				offline: 0,
				loginParams: {
					userName: '',
					passWord: '',
				
				},
			}
		},
		methods: {
			// async getLogin() {
			// 	let res = await login()
			// 	console.log(res);
			// }
		
		
			submit(ref) {
				log(this.isChecked)
				if(this.isChecked){
					setStorageSync('token',this.isChecked)
					useRouter('/pages/indexv4/index', {}, 'navigateTo')
					return
				}
				this.$refs[ref].validate().then(res => {
					login(res).then(res => {
						setStorageSync('token', res.token)
						if (res.code === 200) {
							// uni.switchTab({
							// 	url: '/pages/index/index'
							// })
							useRouter('/pages/indexv4/index', {}, 'navigateTo')
						}
					})
				}).catch(err => {
					console.log('err', err);
				})
			},
			setupHandle() {
				uni.navigateTo({
					url:'/pages/setting/indexv4'
				})
			},
		}
	}
</script>

<style lang="scss">
	.container{
		height: 1300rpx;
		background-color: #005500;
	}
	.login-img {
		width: 100%;
	}

	.login-user {
		margin: 40rpx auto;
		width: 60%;


	}

	.login-password {
		margin: 40rpx auto;
		width: 60%;

	}

	.login {
		width: 40%;
		margin-left: 10%;
		margin-top: 10%;
		float: left;
		color: white;
		background-color: #55aaff;
	}

	.setup {
		width: 40%;
		margin-left: 4%;
		float: left;
		margin-top: 10%;
		color: white;
		background-color: #55aaff;
	}
</style>